﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Serialization;
using System.IO;

namespace DOJ.LEISP.LEXS.Common.Utilities
{
	/// <summary>
	/// Utility class to assist with serialization
	/// </summary>
	public sealed class XmlSerializerUtility
	{
		/// <summary>
		/// Deserializes the XML string to type T
		/// </summary>
		/// <typeparam name="T">The target type</typeparam>
		/// <param name="xml">A valid string of XML to deserialize</param>
		/// <returns>An instance of type T with members hydrated from the XML input string</returns>
		public static T Deserialize<T>(string xml)
		{
			XmlSerializer xsd = new XmlSerializer(typeof(T));
			XmlReader reader = null;
			T deserializedObject;
			XmlReaderSettings settings = new XmlReaderSettings { IgnoreWhitespace = true, IgnoreComments = true };
			reader = XmlReader.Create(new StringReader(xml), settings);
			if (reader.Read())
				deserializedObject = (T)(xsd.Deserialize(reader));
			else
				deserializedObject = default(T);
			return deserializedObject;
		}
	}  

}
